/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

package com.xjw.service;

import com.xjw.entity.ChessGameInfo;
import com.xjw.mapper.ChessMapper;
import com.xjw.tool.ArrayUtil;
import com.xjw.tool.MybatisUtils;

import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.data.DataSet;
import org.neuroph.nnet.Perceptron;

import java.util.Arrays;
import java.util.List;

/**
 * 五子棋网络
 *
 * @author xwx1052336
 * @since 2021/10/12
 */
public class ChessNeuroph {

    public static void main(String[] args) {
        DataSet trainingSet = new DataSet(15*15, 2);
        ChessMapper chessMapper = MybatisUtils.getMapper(ChessMapper.class);
        List<ChessGameInfo> chessGames = chessMapper.getAllChessGame();
        for(ChessGameInfo chessGame:chessGames) {
            double[] input = ArrayUtil.changeArryDimension(ArrayUtil.stringToArray(chessGame.getMap(), 15,15));
            double[] output = {chessGame.getChessX(),chessGame.getChessY()};
            trainingSet.addRow(input, output);
        }
        NeuralNetwork chessPerceptron = new Perceptron(15*15, 2);
        // 学习这个训练集，就是为了得到神经网络的参数
        chessPerceptron.learn(trainingSet);
        // 测试这个感知机，检验它训练的参数是否正确
        chessPerceptron.setInput(ArrayUtil.changeArryDimension(ArrayUtil.stringToArray(chessGames.get(1).getMap(), 15,15)));
        chessPerceptron.calculate();
        System.out.println(" Output: " + Arrays.toString(chessPerceptron.getOutput()));
    }

}
